// MValueListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "MarionetteStudioDll.h"
#include "MValueListCtrl.h"

#include <sstream>
#include <iomanip>

// CMValueListCtrl
using namespace std;
IMPLEMENT_DYNAMIC(CMValueListCtrl, CListCtrl)

CMValueListCtrl::CMValueListCtrl()
{

}

CMValueListCtrl::~CMValueListCtrl()
{
}


BEGIN_MESSAGE_MAP(CMValueListCtrl, CListCtrl)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CMValueListCtrl message handlers




int CMValueListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	CreateColumn();

	this->ModifyStyleEx(LVS_EX_FULLROWSELECT,0);
	return 0;
}
std::wstring s2ws(const std::string& s)
{
	setlocale(LC_ALL, "chs"); 
	const char* _Source = s.c_str();
	size_t sourceSize = s.size() + 1;
	size_t _Dsize = s.size() + 1;
	wchar_t *_Dest = new wchar_t[_Dsize];
	wmemset(_Dest, 0, _Dsize);
	mbstowcs_s(&_Dsize,_Dest,_Dsize,_Source,sourceSize);
	std::wstring result = _Dest;
	delete []_Dest;
	setlocale(LC_ALL, "C");
	return result;
}
int CMValueListCtrl::SetMValue( Marionette::MString name,Marionette::MValue v )
{
	int i = InsertItem(0,s2ws(name).c_str());
	std::wstringstream ss;
	ss<<_T("0x")<<hex<<uppercase<<std::setw(8)<<std::setfill(_T('0'))<<v.Address();

	SetItemText(i,1,s2ws(v.Type()->Name()).c_str());
	SetItemText(i,2,ss.str().c_str());
	
	//SetItemText(i,3,s2ws(v.ToString()).c_str());
	return i;
}

void CMValueListCtrl::CreateColumn()
{
	struct ColStruct{
		TCHAR* pName;
		int nFormat;
		int nWidth;
		int SubItem;
	};
	ColStruct colsData[]={
		{_T("Name"),LVCFMT_LEFT,120,-1},
		{_T("Type"),LVCFMT_LEFT,60,-1},
		{_T("Address"),LVCFMT_LEFT,100,-1},
		{_T("Value"),LVCFMT_LEFT,100,-1}
	};
	for(int i=0;i<sizeof(colsData)/sizeof(colsData[0]);i++)
	{
		InsertColumn(i,colsData[i].pName,colsData[i].nFormat,colsData[i].nWidth,colsData[i].SubItem);
	}
}